﻿Option Strict On
Imports System.Runtime.Serialization
''' <summary>
''' 
''' </summary>
''' <remarks></remarks>
'''  <history>
''' 	[bdesjardins]	2010-03	Created
''' </history>
<Serializable()> _
Public Class ExtendedDataTable
    Inherits DataTable

#Region " Fields "
    Private _adapter As New SqlDataAdapter()
    Private _commandName As String
    Private metaQuery As MetaQuery
#End Region

#Region " Methode "
    ''' <summary>
    '''     Creates a new ExtendedDataTable by initializing it's adapter.
    '''     Defines what data will be load into the DataTable and how it will
    '''     be managed.
    ''' </summary>
    ''' <param name="adapter"></param>
    ''' <remarks>The SqlDataAdapter should have a valid SelectCommand and include a rowversion  (alias timestamp) field</remarks>
    Public Sub New(ByVal adapter As SqlDataAdapter)
        Me.New(adapter, adapter.SelectCommand.CommandText)
    End Sub
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks>Mandatory if we need to use GetChanges</remarks>
    Public Sub New()
        MyBase.new()
        Dim command As SqlCommand = Base.Connection.Command
        Me.Adapter = New SqlDataAdapter(command)
    End Sub
    ''' <summary>
    '''     Creates a new ExtendedDataTable by initializing it's adapter.
    '''     Defines what data will be load into the DataTable and how it will
    '''     be managed.
    ''' </summary>
    ''' <param name="adapter"></param>
    ''' <param name="command">MetaCommand (select)</param>
    ''' <remarks>The "select" statement in the command parameter must include a timestamp (alias rowversion) field</remarks>
    Public Sub New(ByVal adapter As SqlDataAdapter, ByVal command As String)
        MyBase.new()
        Me.CommandName = command
        Me.Adapter = adapter
        '
        Me.Adapter.SelectCommand.CommandText = Me.CommandName
        Me.Adapter.AcceptChangesDuringUpdate = True   'Do not Remove... If Remove Can't trap OnUpdatedRow
        Me.Adapter.Fill(Me)
    End Sub
    Public Sub New(ByVal tableName As String)
        MyBase.New(tableName)

    End Sub
    Public Sub New(ByVal tableName As String, ByVal tableNamespace As String)
        MyBase.New(tableName, tableNamespace)

    End Sub
    Protected Sub New(ByVal info As System.Runtime.Serialization.SerializationInfo, ByVal context As System.Runtime.Serialization.StreamingContext)
        MyBase.New(info, context)

    End Sub

    ''' <summary>
    ''' From the SELECT command generate the INSERT, UPDATE and DELETE command
    ''' </summary>
    ''' <remarks>Property must have a TStamp or RecordVersion</remarks>
    Public Sub BuildCUDCommands(ByVal metaQuery As MetaQuery)
        'keep a copy of the original SELECT command. / Might be an SP Buy design
        Dim oldCmdText As String = Me.Adapter.SelectCommand.CommandText
        Dim oldCmdType As CommandType = Me.Adapter.SelectCommand.CommandType
        'Here we Assign Metaquery for updating RowVersion, later.
        Me.metaQuery = metaQuery
        Me.Adapter.SelectCommand.CommandType = CommandType.Text
        Me.Adapter.SelectCommand.CommandText = metaQuery.SQLStatement
        'To use the CompareRowVersion option, the "select" command must include
        'a rowversion (alias timestamp ) field.
        'Refer to : 
        'If not, there will be errors when
        'trying to build the Delete/Insert/Update commands below. This field will
        'be use as aconcurrency indicator.
        '
        Dim builder As New SqlCommandBuilder(Me.Adapter)
        builder.ConflictOption = ConflictOption.CompareRowVersion
        With Me.Adapter
            Try
                .DeleteCommand = builder.GetDeleteCommand(True)
                .InsertCommand = builder.GetInsertCommand(True)
                .UpdateCommand = builder.GetUpdateCommand(True)
            Catch ex As Exception
                Throw New Exception("Error in BuildCUDCommands MetaQuery: " + metaQuery.QueryName, ex)
            Finally
                'Reassigne the original SQL command
                Me.Adapter.SelectCommand.CommandType = oldCmdType
                Me.Adapter.SelectCommand.CommandText = oldCmdText
            End Try
        End With
    End Sub
#End Region

#Region " Properties "
    Public Property Adapter() As SqlDataAdapter
        Get
            Return _adapter
        End Get
        Set(ByVal value As SqlDataAdapter)
            _adapter = value
        End Set
    End Property
    ''' <summary>
    ''' Select command
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property CommandName() As String
        Get
            Return _commandName
        End Get
        Set(ByVal value As String)
            _commandName = value
        End Set
    End Property
#End Region

End Class
